<?php
require '../db_tools.php';
session_start();
////////////////////////////////////////////////////////////
/// 之所以有这一步是因为后面存钱、取钱、转账的时候，有需要通过post传值，但最开始的post的值和会被后面post的值给顶掉，
/// 导致跳转回本页面的时候，没办法获取到刚开始登录的post的值，导致本页面加载出错
if($_COOKIE['userkey']!=null){
    $key=$_COOKIE['userkey'];
}
if(empty($_POST['username'])&&empty($_POST['userpwd'])){
    if(isset($_SESSION[$key])){
        $user_name=$_SESSION[$key]['user_name'];
        $user_pwd=$_SESSION[$key]['user_pwd'];
    }
}
else{
    $user_name=$_POST['username'];
    $user_pwd=$_POST['userpwd'];
}
/////////////////////////////////////////////////////////////

if(!empty($user_name) && !empty($user_pwd)){
    $sql='SELECT * FROM bankcard WHERE CardNo='.$user_name.' AND CardPwd='.$user_pwd;
    $conn=new mysqli();
    $conn->connect('127.0.0.1','root','root','bank');
    $db_tool=new db_tools();
    $db_tool->set_coonnect($conn);
    $data=$db_tool->select($sql);
//var_dump($data);
    if($data[0]==null){
        echo '用户名或者账号错误<br/>';
        echo '<a href="loginUI.php.php">重新登陆</a>';
        die;
    }else{
        $sql2='SELECT * FROM accountinfo WHERE AccountId=(SELECT AccountId FROM bankcard WHERE CardNo='.$user_name.' AND CardPwd='.$user_pwd.')';
        $data2=$db_tool->select($sql2);
        $card_money;
        echo "欢迎您:".$user_name;
        echo '<br/>';
        for ($i=0;$i<count($data);$i++){
            print_r('您当前的用户信息：<br/>');
            print_r( '用户id:'.$data[$i]['AccountId'].'<br/>用户名:'.$data2[$i]['RealName'].'<br/>卡id:'.$data[$i]['CardId'].'<br/>卡号：'.$data[$i]['CardNo'].'<br/>余额：'.$data[$i]['CardMoney']);
            $card_money=$data[$i]['CardMoney'];
        }
        echo '<br/><a href="../money/money_in.php"><button>存钱</button></a>  ';
        echo '<a href="../money/money_out.php"><button>取钱</button></a>  ';
        echo '<a href="../money/Transfer.php"><button>转账</button></a><br/>';
        echo '<a href="../card/select_card.php"><button>查询本人下的其它卡的信息</button></a><br/>';
        echo '<a href="../card/delete_card.php"><button>销卡</button></a><br/>';
        setcookie('userkey',md5($user_name.$user_pwd),time()+24*3600,'/');
        $_SESSION[md5($user_name.$user_pwd)]=[
            'user_name'=>$user_name,
            'user_pwd'=>$user_pwd,
            'card_money'=>$card_money
        ];
    };
}
else{
    echo '账号密码不存在,<a href="loginUI.php">重新登陆</a>';
}


